$SAVE
$NOLIST
;************************************************************************
;*                                                                      *
;*    R T X - 51  :  MACRO: REMOVE TASK FROM READYLIST                  *
;*                                                                      *
;*----------------------------------------------------------------------*
;*                                                                      *
;*    Filename     :   RTXREMOV.INC                                     *
;*    Language:        Keil A-51                                        *
;*    Dev. system:     Keil uVision2                                    *
;*    Targetsystem :   Any system based upon 8051 up                    *
;*                                                                      *
;*    Purpose      :   Macro definition: remove task from ready list.   *
;*                     To be used as an include file.                   *
;*                                                                      *
;*----------------------------------------------------------------------*
;* Rev. |  Released   | Programmer    | Comments                        *
;*----------------------------------------------------------------------*
;*  0.1 |  4.1.1990   | E. Glatz      | Changes for V3.18               *
;*  0.2 |  7.3.1991   | Th. Fischler  | Changes for V4.0                *
;*  0.3 |   8.05.96   | E. Glatz      | Jump to ?RTX_IDLE_FUNC when idle*
;* 7.00 |  13.11.2001 | T. Mueller    | Release 7.00                    *
;************************************************************************
;*    Copyright 1991 .. 2001 METTLER  &  FUCHS  AG,  CH-8953 Dietikon   *
;************************************************************************

REMOVE_RDY_PRIO   MACRO
      LOCAL IDLE_LOOP,PRIO0,PRIO1,PRIO2,PRIO3,START,ENDL,PR0E,PR1E,PR2E,PR3E
;;
;;       REMOVE TASK OF HIGHEST PRIORITY FROM READY_LIST, RETURN IT IN ACC
;;       -----------------------------------------------------------------
;;
;;       Removes the ready task with the highest priority from the ready
;;       list and returns its task_id in ACC.
;;       Supported are 4 different priorities and max. 16 active tasks.
;;
;;       Format:
;;
;;               REMOVE_RDY_PRIO
;;
;;
;;       SIDE EFFECTS : uses ACC and DPTR
;;                      before entering an EXCLUDE_ALL_INT must be issued
;;
;
;       REMOVE_RDY_PRIO
;       (Return task_id in ACC)
;
START:         CLR   A
               CJNE  A,?RTX_RDY_COUNT3, PRIO3        ; Search for highest
               CJNE  A,?RTX_RDY_COUNT2, PRIO2        ; prio. ready task
               CJNE  A,?RTX_RDY_COUNT1, PRIO1
               CJNE  A,?RTX_RDY_COUNT0, PRIO0
;;Ready_list is empty: wait for an interrupt-event
               MOV   A, #NIL_TID
               MOV   ?RTX_RUNNING_TID, A
               $IF (DBG = 02H)
                  MOV   DPTR,#DEBUG_TID
                  MOVX  @DPTR,A
               $ENDIF
               DBG_IDLE
               RTX_ALLOW_INT
IDLE_LOOP:     LJMP  ?RTX_IDLE_FUNC                  ; If ready_list is empty
                                                     ; wait for interrupt-event
                                                     ; (jump to idle function)
PRIO0:         DEC   ?RTX_RDY_COUNT0                 ; Remove (task_id) from
               MOV   DPL, ?RTX_RDY_FIRST0            ; priority 0 FIFO
               MOV   DPH, #HIGH(?RTX_SYS_PAGE)
               MOV   A, ?RTX_RDY_FIRST0              ; increment pointer and
               CJNE  A, #LOW(?RTX_RDY_PRIO0_END-1), PR0E  ; test for overflow
               MOV   ?RTX_RDY_FIRST0, #LOW(?RTX_RDY_PRIO0-1)
PR0E:          INC   ?RTX_RDY_FIRST0
               MOVX  A, @DPTR
               SJMP  ENDL

PRIO1:         DEC   ?RTX_RDY_COUNT1                 ; Remove (task_id) from
               MOV   DPL, ?RTX_RDY_FIRST1            ; priority 1 FIFO
               MOV   DPH, #HIGH(?RTX_SYS_PAGE)
               MOV   A, ?RTX_RDY_FIRST1              ; increment pointer and
               CJNE  A, #LOW(?RTX_RDY_PRIO1_END-1), PR1E  ; test for overflow
               MOV   ?RTX_RDY_FIRST1, #LOW(?RTX_RDY_PRIO1-1)
PR1E:          INC   ?RTX_RDY_FIRST1
               MOVX  A, @DPTR
               SJMP  ENDL

PRIO2:         DEC   ?RTX_RDY_COUNT2                 ; Remove (task_id) from
               MOV   DPL, ?RTX_RDY_FIRST2            ; priority 2 FIFO
               MOV   DPH, #HIGH(?RTX_SYS_PAGE)
               MOV   A, ?RTX_RDY_FIRST2              ; increment pointer and
               CJNE  A, #LOW(?RTX_RDY_PRIO2_END-1), PR2E  ; test for overflow
               MOV   ?RTX_RDY_FIRST2, #LOW(?RTX_RDY_PRIO2-1)
PR2E:          INC   ?RTX_RDY_FIRST2
               MOVX  A, @DPTR
               SJMP  ENDL

PRIO3:         DEC   ?RTX_RDY_COUNT3                 ; Remove (task_id) from
               MOV   DPL, ?RTX_RDY_FIRST3            ; priority 3 FIFO
               MOV   DPH, #HIGH(?RTX_SYS_PAGE)
               MOV   A, ?RTX_RDY_FIRST3              ; increment pointer and
               CJNE  A, #LOW(?RTX_RDY_PRIO3_END-1), PR3E  ; test for overflow
               MOV   ?RTX_RDY_FIRST3, #LOW(?RTX_RDY_PRIO3-1)
PR3E:          INC   ?RTX_RDY_FIRST3
               MOVX  A, @DPTR
ENDL:
               ENDM

$RESTORE
